#include <QAction>
#include <QMenu>
#include "SystemTrayIcon.h"
#include "platform/common.h"
#include "ConfigData.h"
#include "ConfigEditor.h"

SystemTrayIcon::SystemTrayIcon(QObject *parent) : QSystemTrayIcon (parent), iconLive(":/icon-live.png"), iconGrey(":/icon-grey.png")
{
    initUI();
    connectSignals();
    onModeChanged();
}

SystemTrayIcon::~SystemTrayIcon()
{
    menu->deleteLater();
}

void SystemTrayIcon::initUI()
{
    ce = nullptr;
    menu = new QMenu;

    modeSubMenu = new QMenu(tr("Mode"), menu);
    changeModeAllAction = new QAction(tr("All"), this);
    changeModeCustomAction = new QAction(tr("Custom"), this);
    changeModeNoneAction = new QAction(tr("None"), this);

    modeSubMenu->addAction(changeModeAllAction);
    modeSubMenu->addAction(changeModeCustomAction);
    modeSubMenu->addAction(changeModeNoneAction);

    searchAction = new QAction(tr("SearchBar"), this);
    ocrTextAction = new QAction(tr("OCR Text"), this);
    ocrTranslateAction = new QAction(tr("OCR Translate"), this);

    configAction = new QAction(tr("Config"), this);
    aboutAction = new QAction(tr("About"), this);

    helpSubMenu = new QMenu(tr("Help"), menu);
    guideAction = new QAction(tr("Guide"), this);
    manualAction = new QAction(tr("Mannual"), this);
    homepageAction = new QAction(tr("HomePage"), this);
    donateAction = new QAction(tr("Donate"), this);
    getCDKeyAction = new QAction(tr("Get CDKey"), this);
    checkUpdateAction = new QAction(tr("Check Update"), this);

    helpSubMenu->addAction(guideAction);
    helpSubMenu->addAction(manualAction);
    helpSubMenu->addAction(homepageAction);
    helpSubMenu->addAction(donateAction);
    helpSubMenu->addAction(getCDKeyAction);
    helpSubMenu->addAction(checkUpdateAction);

    feedbackAction = new QAction(tr("Feedback"), this);
    quitAction = new QAction(tr("Quit"), this);

    // menu
    menu->addMenu(modeSubMenu);

    menu->addAction(searchAction);
    menu->addAction(ocrTextAction);
    menu->addAction(ocrTranslateAction);
    menu->addAction(configAction);
    menu->addAction(aboutAction);

    menu->addMenu(helpSubMenu);
    menu->addAction(feedbackAction);

    menu->addAction(quitAction);

    setContextMenu(menu);
    setIcon(iconLive);
}

void SystemTrayIcon::connectSignals()
{
    connect(quitAction, &QAction::triggered, this, &SystemTrayIcon::onQuitAction);
    connect(configAction, &QAction::triggered, this, &SystemTrayIcon::onConfigAction);
    connect(searchAction, &QAction::triggered, ConfigData::instance().translate->searchBarShortcut, &ItemShortcut::trigger);
    connect(ocrTextAction, &QAction::triggered, ConfigData::instance().translate->OCRTextShortcut, &ItemShortcut::trigger);
    connect(ocrTranslateAction, &QAction::triggered, ConfigData::instance().translate->OCRTranslateShortcut, &ItemShortcut::trigger);

    connect(ConfigData::instance().pickMode, &ItemEnum::itemEdited, this, &SystemTrayIcon::onModeChanged);
    connect(changeModeAllAction, &QAction::triggered, this, &SystemTrayIcon::onModeChanged);
    connect(changeModeCustomAction, &QAction::triggered, this, &SystemTrayIcon::onModeChanged);
    connect(changeModeNoneAction, &QAction::triggered, this, &SystemTrayIcon::onModeChanged);
}

void SystemTrayIcon::setModeText(QAction *modeAction, bool checked)
{
    QString text = modeAction->text();
    if (checked && !text.endsWith("  ✓"))
        modeAction->setText(text.append("  ✓"));
    else if (!checked && text.endsWith("  ✓"))
        modeAction->setText(text.remove("  ✓"));
}

void SystemTrayIcon::onQuitAction()
{
    Platform::monitor().stopMonitor();
    qApp->quit();
}

void SystemTrayIcon::onConfigAction()
{
    if (ce)
    {
        ce->raise();
        return;
    }

    ce = new ConfigEditor;
    ce->setAttribute(Qt::WA_DeleteOnClose, true);
    ce->show();
    connect(ce, &ConfigEditor::aboutToClose, this, &SystemTrayIcon::onConfigEditorClosed);
}

void SystemTrayIcon::onModeChanged()
{
    auto pickMode = ConfigData::instance().pickMode;

    if (sender() == changeModeAllAction)
    {
        pickMode->set(ConfigData::All);
    }
    else if (sender() == changeModeCustomAction)
    {
        pickMode->set(ConfigData::Custom);
    }
    else if (sender() == changeModeNoneAction)
    {
        pickMode->set(ConfigData::None);
    }
    else
    {
        switch (pickMode->get())
        {
        case ConfigData::All:
            setModeText(changeModeAllAction, true);
            setModeText(changeModeCustomAction, false);
            setModeText(changeModeNoneAction, false);
            break;
        case ConfigData::Custom:
            setModeText(changeModeAllAction, false);
            setModeText(changeModeCustomAction, true);
            setModeText(changeModeNoneAction, false);
            break;
        case ConfigData::None:
            setModeText(changeModeAllAction, false);
            setModeText(changeModeCustomAction, false);
            setModeText(changeModeNoneAction, true);
            break;
        }
    }
}

void SystemTrayIcon::onConfigEditorClosed()
{
    ce = nullptr;
}
